var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
(function (factory) {
    if (typeof module === "object" && typeof module.exports === "object") {
        var v = factory(require, exports);
        if (v !== undefined) module.exports = v;
    }
    else if (typeof define === "function" && define.amd) {
        define(["require", "exports", "../../services/service", "maishu-chitu", "maishu-services-sdk", "js-md5", "../config"], factory);
    }
})(function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    const service_1 = require("../../services/service");
    const chitu = require("maishu-chitu");
    const maishu_services_sdk_1 = require("maishu-services-sdk");
    const md5 = require("js-md5");
    const config_1 = require("../config");
    exports.userInfo = new chitu.ValueStore();
    class MemberService extends service_1.Service {
        constructor() {
            super();
        }
        url(path) {
            return `${config_1.config.memberUrl}${path}`;
        }
        userInfo() {
            let url = this.url(`User/CurrentUserInfo`); //this.url('User/CurrentUserInfo');
            return this.get(url);
        }
        saveUserInfo(userInfo) {
            let url = this.url('/Member/SaveUserInfo');
            return this.putByJson(url, userInfo);
        }
        sentVerifyCode(mobile, type) {
            console.assert(mobile != null);
            let url = `sms/sendVerifyCode`;
            return this.putByJson(url, { mobile, type });
        }
        checkVerifyCode(smsId, verifyCode) {
            let url = `sms/checkVerifyCode`;
            return this.getByJson(url, { smsId, verifyCode });
        }
        /** 发送验证码到指定的手机 */
        sentRegisterVerifyCode(mobile) {
            console.assert(mobile != null);
            // let url = `UserMember/SMS/SendRegisterVerifyCode`;
            let url = 'user';
            return this.postByJson(url, { mobile, type: 'register' });
            // return this.sentVerifyCode(mobile, 'reigster');
        }
        /** 用户注册 */
        register(mobile, password, smsId, verifyCode) {
            return __awaiter(this, void 0, void 0, function* () {
                password = md5(password);
                let ps = this.createService(maishu_services_sdk_1.PermissionService);
                let r = yield ps.user.register(mobile, password, smsId, verifyCode);
                service_1.Service.token.value = r.token;
            });
        }
        login(username, password) {
            return __awaiter(this, void 0, void 0, function* () {
                let ps = this.createService(maishu_services_sdk_1.PermissionService);
                password = md5(password);
                let r = yield ps.user.login(username, password);
                service_1.Service.token.value = r.token;
                return r;
            });
        }
        resetPassword(mobile, password, smsId, verifyCode) {
            let url = `user/resetPassword`;
            return this.putByJson(url, { mobile, password, smsId, verifyCode }).then(data => {
                return data;
            });
        }
        changePassword(password, smsId, verifyCode) {
            let url = `user/changePassword`;
            return this.putByJson(url, { password, smsId, verifyCode }).then(data => {
                return data;
            });
        }
        changeMobile(mobile, smsId, verifyCode) {
            let url = `user/changeMobile`;
            return this.putByJson(url, { mobile, smsId, verifyCode });
        }
        store() {
            return __awaiter(this, void 0, void 0, function* () {
                if (this._store) {
                    return this._store;
                }
                let url = this.url("Application/Item");
                let store = yield this.getByJson(url, { id: this.applicationId });
                if (store == null)
                    return null;
                this._store = store;
                return this._store;
            });
        }
    }
    exports.MemberService = MemberService;
});
